package cn.itsource.hrm.web.controller;

import cn.itsource.hrm.domain.User;
import cn.itsource.hrm.service.IUserService;
import cn.itsource.hrm.query.UserQuery;
import cn.itsource.hrm.result.JSONResult;
import cn.itsource.hrm.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
    * 保存和修改操作公用此方法
    * @param user 前端传递来的实体数据
    */
    @PostMapping(value="/save")
    public JSONResult save(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JSONResult.success();
    }

    /**
    * 根据ID删除指定对象信息
    * @param id
    */
    @DeleteMapping(value="/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JSONResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id)
    {
        return JSONResult.success(userService.selectById(id));
    }

    //清空用户信息
    @GetMapping(value = "/deleteAllUser")
    public JSONResult get()
    {
        userService.deleteAllUser();
        return JSONResult.success();
    }

    //生成count条用户信息数据到数据库中
    @GetMapping(value = "/addUser/{count}")
    public JSONResult addUser(@PathVariable("count") Integer count)
    {
        userService.addUser(count);
        return JSONResult.success();
    }

    //生成count条用户信息数据到数据库中
    @GetMapping(value = "/addUserThreadPool/{count}")
    public JSONResult addUserThreadPool(@PathVariable("count") Integer count)
    {
        userService.addUserThreadPool(count);
        return JSONResult.success();
    }

    /**
    * 查看所有对象数据（不分页）
    */
    @GetMapping(value = "/list")
    public JSONResult list(){
        return JSONResult.success(userService.selectList(null));
    }


    /**
    * 分页查询数据
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping(value = "/pagelist")
    public JSONResult pageList(@RequestBody UserQuery query)
    {
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JSONResult.success(new PageList<User>(page.getTotal(), page.getRecords()));
    }
}
